package com.niit.bigdata.util;

import com.niit.bigdata.entity.Order;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class OrderGenerator {
    private static final String[] CATEGORIES = {"电子产品", "服装", "图书", "家用电器", "玩具"};
    private static final Map<String, String[]> PRODUCT_NAMES = new HashMap<>();

    static {
        PRODUCT_NAMES.put("电子产品", new String[]{"笔记本电脑", "智能手机", "耳机", "相机"});
        PRODUCT_NAMES.put("服装", new String[]{"T 恤", "牛仔裤", "连衣裙", "夹克"});
        PRODUCT_NAMES.put("图书", new String[]{"小说", "非小说", "悬疑", "科学"});
        PRODUCT_NAMES.put("家用电器", new String[]{"冰箱", "洗衣机", "微波炉"});
        PRODUCT_NAMES.put("玩具", new String[]{"动作人偶", "棋盘游戏", "玩偶", "积木"});
    }

    public static Order generateRandomOrder() {
        Random random = new Random();
        String orderCategory = CATEGORIES[random.nextInt(CATEGORIES.length)];
        String[] productNames = PRODUCT_NAMES.get(orderCategory);
        String productName = productNames[random.nextInt(productNames.length)];
        int quantity = random.nextInt(100) + 1;
        Date orderDate = generateRandomDate();
        boolean isValid = random.nextBoolean();
        return new Order(orderCategory, productName, quantity, orderDate, isValid);
    }

    private static Date generateRandomDate() {
        try {
            // 用当前日期替换开始日期
            Date currentDate = new Date();
            long startTime = currentDate.getTime();

            long currentTime = System.currentTimeMillis();
            long randomTime = (long) (Math.random() * (currentTime - startTime)) + startTime;
            return new Date(randomTime);
        } catch (Exception e) {
            e.printStackTrace();
            return new Date();
        }
    }
}
